<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Society extends MY_Controller {
	
	function __construct() {
        parent::__construct();
        $this->load->model('user_model', '', TRUE);
		$this->load->model('society_model', '', TRUE);
        $this->load->model('association_model', '', TRUE);
        $this->load->model('news_model', '', TRUE);
        $this->load->model('announcement_model', '', TRUE);
        $this->load->model('photo_model', '', TRUE);
    }
	
	function society_list() {
		$condition['sort_view_count'] = DESC;
		$condition['offset'] = '';
		$condition['per_page'] = "5";
		$data['hot_society'] = $this->society_model->search_by_condition($condition);
		$data['current_menu_tab'] = MENU_TAB_F;
		
		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		$data['html_title'] = MENU_TAB_F_NAME;
		$this->load->view('frontend/society/society_list', $data);
	}
	
	/**
	 * 社团类别简介
	 * @param $cid
	 */
	function category_intro($cid) {
		
	}
	
	/**
     * 社团主页
     * @param $society_id
     */
	function home($society_id = '') {
		$data['current_menu_tab'] = MENU_TAB_F;
		
		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		$condition['society_id'] = $society_id;
		$data['society'] = $this->society_model->search_by_condition($condition, TRUE);
		
		$condition['society_id'] = $society_id;
		$condition['category'] = SOCIETY_LOGO;
		$temp_data1 = $this->photo_model->search_by_condition($condition, TRUE);
		if ($temp_data1 != "") {
			$data['logo_thumb_path'] = $temp_data1['thumb_path'];
		}
		
		$condition['society_id'] = $society_id;
		$condition['category'] = SOCIETY_SHOW;
		$temp_data2 = $this->photo_model->search_by_condition($condition);
		if ($temp_data2 != "") {
			foreach ($temp_data2 as $k => $v) {
				$data['show_thumb_path'][$k] = $v['thumb_path'];
			}
		}
		
		//获得新闻 公告 开始
		$contents_condition['author_id'] = $society_id;
		$contents_condition['is_published'] = IS_PUBLISHED;
		$contents_condition['sort_create_time'] = DESC;
		$data['news'] = $this->news_model->search_by_condition($contents_condition);
		$data['news_count'] = $this->news_model->search_by_condition($contents_condition, "", TRUE);
		$data['announcement'] = $this->announcement_model->search_by_condition($contents_condition);
		
		//echo "<pre>";
		//print_r($temp_data3);
		//return ;
		$data['html_title'] = $data['society']['s_name'];
		$this->load->view('frontend/society/home', $data);
	}
	
	function recommond_news() {
		//新闻推荐
		$condition['sort_view_count'] = DESC;
		$condition['offset'] = '';
		$condition['per_page'] = 8;
		$data = $this->news_model->search_by_condition($condition);
		return $data;
	}
	
	function news_list($param = "0-0") {
		$url_array = explode("-", $param);
		$data['url_array'] = $url_array;
		
		$data['recommend_news'] = $this->recommond_news();
		
		$data['current_menu_tab'] = MENU_TAB_E;
    	
		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
    	$condition = $this->_get_condition($url_array);
    	
    	$condition['is_published'] = IS_PUBLISHED;
		//获得社团新闻（cid == 1）
		$condition['category'] = SOCIETY_UCID;
		if ($url_array[1] == "0") {
			$condition['sort_create_time'] = DESC;
		}
		
		$data['society_news_count'] = $this->news_model->search_by_condition($condition, "", TRUE);
    	
		//---------------------------------- 分页开始 ------------------------------------//
        $this->load->library('pagination');
        $config['base_url'] = SOCIETY_NEWS_LIST_PATH .$param;
        $config['total_rows'] = $data['society_news_count'];
        $config['uri_segment'] = 5;
        $config['per_page'] = PER_PAGE;
        //$config['num_links'] = 5;  //页码链接偏移量
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = "class='anchor_button' style='font-size: 80%;'";
        //$config['suffix'] = "#pagefocus";
        
        $this->pagination->initialize($config);

		$condition['per_page'] = PER_PAGE;
		$condition['offset'] = $this->uri->segment(5);
		
		$data['society_news'] = $this->news_model->search_by_condition($condition);
		//---------------------------------- 分页结束 ------------------------------------//
		if ($url_array[0] != 0) {
			$data['search_author'] = $this->society_model->_get_full_name_by_society_id($url_array[0]);
		}
		if (!empty($data['society_news'])) {
			foreach ($data['society_news'] as $k => $v) {
				$data['society_news'][$k]['author_abbr'] = $this->society_model->_get_abbr_by_society_id($data['society_news'][$k]['author_id']);
			}
		}
		
		$data['html_title'] = MENU_TAB_E_NAME;
		$this->load->view('frontend/society/news_list', $data);
	}
	
	function news_detail($id) {
		$condition['is_published'] = IS_PUBLISHED;
    	$condition['category'] = SOCIETY_UCID;
		$condition['id'] = $id;
		$data['news_detail'] = $this->news_model->search_by_condition($condition, TRUE);
		if (empty($data['news_detail'])) { show_404(); }
		
		$data['recommend_news'] = $this->recommond_news();
		$data['current_menu_tab'] = MENU_TAB_E;
    	
		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
    	
    	
		$data['news_detail']['author_name'] = $this->society_model->_get_full_name_by_society_id($data['news_detail']['author_id']);
		
		$nearest_condition['category'] = SOCIETY_UCID;
		$nearest_condition['sort_create_time'] = DESC;
		$data['nearest_id'] = $this->_get_nearest_id($id, $nearest_condition, NEWS_TYPE);
		
		$data['html_title'] = $data['news_detail']['title'];
		$this->load->view('frontend/society/news_detail', $data);
	}
	
	/**
	 * 获得当前分类内容的上一篇或下一篇的ID
	 * @param $id
	 * @param $condition
	 * @param $type
	 */
	function _get_nearest_id($id, $condition, $type) {
		switch ($type) {
			case NEWS_TYPE:
				$temp = $this->news_model->search_by_condition($condition, FALSE);
				break;
			case ANNOUNCEMENT_TYPE:
				$temp = $this->announcement_model->search_by_condition($condition, FALSE);
				break;
		}
		
		foreach ($temp as $k => $v) {
			$all_id[] = $v['id'];
			if ($v['id'] == $id) {
				$cur_key = $k;
				$prev_key = $k - 1;
				$next_key = $k + 1;
			}
		}
		if (array_key_exists($prev_key, $all_id)) {
			$nearest_id['prev_id'] = $all_id[$prev_key];
		}
		if (array_key_exists($next_key, $all_id)) {
			$nearest_id['next_id'] = $all_id[$next_key];
		}
		return (isset($nearest_id)) ? $nearest_id : '';
	}
	
	function _get_condition($url_array) {
		
		if ($url_array[0] != 0) {
			$condition['author_id'] = $url_array[0];
		}
    	if ($url_array[1] != 0) {
			if ($url_array[1] == 1) {
				$condition['sort_create_time'] = ASC;
			} elseif ($url_array[1] == 2){
				$condition['sort_create_time'] = DESC;
			} elseif ($url_array[1] == 3) {
				$condition['sort_view_count'] = DESC;
			} else {
				$condition['sort_view_count'] = ASC;
			}
		}
		if (!empty($condition)) {
			return $condition;
		}
    }
    
    function ajax_announcement_detail() {
    	$condition['is_published'] = IS_PUBLISHED;
    	$condition['category'] = SOCIETY_UCID;
		$condition['id'] = $this->input->post('id');
		$data = $this->announcement_model->search_by_condition($condition, TRUE);
		
    	if (empty($data)) { show_404(); }
    	
    	$data['create_time'] = date("Y年m月d日", $data['create_time']);
    	
    	//author info
		$data['author']['name'] = $this->society_model->_get_full_name_by_society_id($data['author_id']);
		$data['author']['href'] = SOCIETY_HOME_PATH . $data['author_id'];
		$data['author']['title'] = $data['author']['name'] . '主页';
		
		$nearest_condition['category'] = SOCIETY_UCID;
		$nearest_condition['sort_create_time'] = DESC;
		$data['nearest_id'] = $this->_get_nearest_id($condition['id'], $nearest_condition, ANNOUNCEMENT_TYPE);
		
		$this->output->set_output(json_encode($data));
	}
	
	function society_festival() {
		
		$data['current_menu_tab'] = MENU_TAB_C;
		
		////////////前台已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		$data['html_title'] = MENU_TAB_C_NAME;
		$this->load->view('frontend/society/society_festival', $data);
	}
	
    
	function ajax_update_home_view_count() {
		$society_id = $this->input->post('society_id');
		if ($this->_update_cookie(COOKIE_SOCIETY_HOME, $society_id)) {
			$this->society_model->update_view_count($society_id);
		}
    }
    
	function ajax_update_news_view_count() {
		$news_id = $this->input->post('news_id');
		if ($this->_update_cookie(COOKIE_NEWS_READ, $news_id)) {
			$this->news_model->update_view_count($news_id);
		}
    }
}